package com.heima.wemedia.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.wemedia.dtos.WmLoginDto;
import com.heima.model.wemedia.pojos.UserInfo;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.utils.common.AppJwtUtil;
import com.heima.utils.common.BCrypt;
import com.heima.wemedia.mapper.WmUserMapper;
import com.heima.wemedia.service.WmUserService;
import org.springframework.stereotype.Service;
@Service
public class WmUserServiceImpl extends ServiceImpl<WmUserMapper, WmUser> implements WmUserService {

    @Override
    public ResponseResult login(WmLoginDto dto) {
        String name = dto.getName();
        WmUser wmUser = this.lambdaQuery().eq(WmUser::getName, name).one();
        if (ObjectUtil.isEmpty(wmUser)){
            return ResponseResult.errorResult(401, "用户不存在");
        }
        boolean checkpw = BCrypt.checkpw(dto.getPassword(), wmUser.getPassword());
        if (checkpw==false){
            return ResponseResult.errorResult(401,"密码错误！");
        }
        wmUser.setPassword("00000");
        String token = AppJwtUtil.getToken(Long.valueOf(wmUser.getId()));
        UserInfo user = UserInfo.builder()
                .user(wmUser)
                .token(token)
                .build();
        return ResponseResult.okResult(user);
    }
}